package com.jyw.business.obstacles.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jyw.business.obstacles.domain.ObsOrderFault;
import com.jyw.business.obstacles.domain.ObsOrderSend;
import com.jyw.business.obstacles.domain.vo.wechat.WxWorkOrderSendVO;
import com.jyw.business.obstacles.mapper.ObsOrderSendMapper;
import com.jyw.business.obstacles.service.IObsOrderSendService;
import com.jyw.common.constant.Constants;
import com.jyw.common.utils.DateUtils;
import com.jyw.system.domain.SysUserDept;
import com.jyw.system.domain.SysUserDeptRole;
import com.jyw.system.domain.SysUserTeam;
import com.jyw.system.service.ISysTeamService;
import com.jyw.system.service.ISysUserDeptRoleService;
import com.jyw.system.service.ISysUserDeptService;
import com.jyw.system.service.ISysUserTeamService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;

/**
 * 报障-派单记录Service业务层处理
 * 
 * @author system
 * @date 2023-08-11
 */
@Service
public class ObsOrderSendServiceImpl extends ServiceImpl<ObsOrderSendMapper, ObsOrderSend> implements IObsOrderSendService
{
    @Resource
    private ObsOrderSendMapper obsOrderSendMapper;

    @Resource
    private ISysUserTeamService userTeamService;

    @Resource
    private ISysUserDeptService userDeptService;

    /**
     * 查询报障-派单记录
     * 
     * @param id 报障-派单记录主键
     * @return 报障-派单记录
     */
    @Override
    public ObsOrderSend selectObsOrderSendById(Long id)
    {
        return obsOrderSendMapper.selectObsOrderSendById(id);
    }

    /**
     * 查询报障-派单记录列表
     * 
     * @param obsOrderSend 报障-派单记录
     * @return 报障-派单记录
     */
    @Override
    public List<ObsOrderSend> selectObsOrderSendList(ObsOrderSend obsOrderSend)
    {
        return obsOrderSendMapper.selectObsOrderSendList(obsOrderSend);
    }

    /**
     * 新增报障-派单记录
     * 
     * @param obsOrderSend 报障-派单记录
     * @return 结果
     */
    @Override
    public int insertObsOrderSend(ObsOrderSend obsOrderSend)
    {
        obsOrderSend.setCreateTime(DateUtils.getNowDate());
        return obsOrderSendMapper.insertObsOrderSend(obsOrderSend);
    }

    /**
     * 修改报障-派单记录
     * 
     * @param obsOrderSend 报障-派单记录
     * @return 结果
     */
    @Override
    public int updateObsOrderSend(ObsOrderSend obsOrderSend)
    {
        obsOrderSend.setUpdateTime(DateUtils.getNowDate());
        return obsOrderSendMapper.updateObsOrderSend(obsOrderSend);
    }

    /**
     * 批量删除报障-派单记录
     * 
     * @param ids 需要删除的报障-派单记录主键
     * @return 结果
     */
    @Override
    public int deleteObsOrderSendByIds(Long[] ids)
    {
        return obsOrderSendMapper.deleteObsOrderSendByIds(ids);
    }

    /**
     * 删除报障-派单记录信息
     * 
     * @param id 报障-派单记录主键
     * @return 结果
     */
    @Override
    public int deleteObsOrderSendById(Long id)
    {
        return obsOrderSendMapper.deleteObsOrderSendById(id);
    }

    @Override
    public boolean isMakeUser(String orderNum, Long userId) {
        //查询派单信息
        List<ObsOrderSend> list = list(new QueryWrapper<ObsOrderSend>()
                .eq(StrUtil.toUnderlineCase(ObsOrderSend.Fields.orderNum), orderNum)
        );
        //查询用户小组信息
        List<SysUserTeam> userTeamList = userTeamService.list(
                new QueryWrapper<SysUserTeam>().eq(StrUtil.toUnderlineCase(SysUserTeam.Fields.userId), userId)
                        .eq(StrUtil.toUnderlineCase(SysUserTeam.Fields.isEnable), Constants.BOOL_TRUE)
        );
        //查询用户部门信息
        List<SysUserDept> userDepts = userDeptService.lambdaQuery().eq(SysUserDept::getUserId,userId).list();


        for (ObsOrderSend send : list) {
            switch (send.getSendType()){
                case 1:
                    //用户
                    if (send.getSendUser().equals(userId)) {
                        return true;
                    }
                    break;
                case 2:
                    //小组
                    if (userTeamList.stream().filter(data->data.getTeamId().equals(send.getSendTeam())).findFirst().isPresent()) {
                        return true;
                    }
                    break;
                case 3:
                    //部门
                    if(userDepts.stream().filter(data->data.getDeptId().equals(send.getSendDept())).findFirst().isPresent()){
                        return true;
                    }
                    break;
            }
        }

        return false;
    }

    @Override
    public List<WxWorkOrderSendVO> queryOrderSendList(String orderNum) {
        return obsOrderSendMapper.queryOrderSendList(orderNum);
    }

    @Override
    public List<ObsOrderFault> listTimeOut(Date timeOutDate) {
        return obsOrderSendMapper.listTimeOut(timeOutDate);
    }
}
